package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import model.view.CasillaView;
import controller.Maquina;
import controller.exception.MaquinaInstantiationException;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CrearComodin extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton botonAceptar;
	private int cantidadCasillas;
	private int posicionCambianteH=110;
	private JTextField jTextField1;
	private JLabel jLabel3;
	private static CrearComodin instancia;
	private Set<CasillaView> casillasView;
	private Map<String,Integer> nombreFrutaCodigo;
	private List<JComboBox> combos=new ArrayList<JComboBox>();

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CrearComodin(3));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public CrearComodin(int cantCasillas) {
		super();
		try {
			casillasView=Maquina.getInstancia().obtenerCasillas();
			this.cantidadCasillas=cantCasillas;
			createMap();
			initGUI();
			
		} catch (MaquinaInstantiationException e) {
			System.out.println("No hay maquina creada");
		}
	}
	

	private void createMap() {
		nombreFrutaCodigo=new HashMap<String,Integer>();
		for(CasillaView view:casillasView){
			nombreFrutaCodigo.put(view.toString(), view.getCodigo());
		}
	}


	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(433, 240));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleccione las casillas que conforman el Comodin");
				jLabel1.setBounds(12, 12, 355, 27);
			}
			
			for (int i=0;i<cantidadCasillas;i++){
				JLabel jLabel3 = new JLabel();
				jLabel3.setText("Posicion " +(i+1));
				jLabel3.setBounds(18, posicionCambianteH, 73, 23);
				ComboBoxModel jComboBoxModel = new DefaultComboBoxModel(Utility.crearOpcionesCasilla(casillasView));
				JComboBox jComboBox = new JComboBox();
				jComboBox.setModel(jComboBoxModel);
				jComboBox.setBounds(275, posicionCambianteH, 200, 23);
				combos.add(jComboBox);
				this.add(jLabel3);
				this.add(jComboBox);
				posicionCambianteH=posicionCambianteH+30;
			}
			{
				jLabel2 = new JLabel();
				jLabel2.setText("");
				jLabel2.setBounds(18, posicionCambianteH+80, 73, 23);
				jLabel2.setVisible(false);
			}
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Agregar Comodin");
				botonAceptar.setBounds(236, posicionCambianteH+80, 131, 37);
				
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt)
					{
						try {
							Maquina.getInstancia().crearCasillaComodin(makeListCasillas(combos,
									nombreFrutaCodigo),jTextField1.getText());
						} catch (MaquinaInstantiationException e) {
							System.out.println("No se recupero la maquina");
						}
					}
				});
				
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Nombre");
				jLabel3.setBounds(33, 51, 62, 16);
			}
			{
				jTextField1 = new JTextField();
				this.add(jTextField1);
				jTextField1.setBounds(183, 51, 149, 23);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	protected static List<Integer> makeListCasillas(List<JComboBox> comboBoxes, Map<String,Integer> nombreFrutaCodigo) {
		List<Integer> frutasId=new ArrayList<Integer>();
		for(int i=0;i<comboBoxes.size();i++){
			String nombreFruta=Utility.getSelectedValue(comboBoxes.get(i));
			frutasId.add(nombreFrutaCodigo.get(nombreFruta));
		}
		return frutasId;
	}

	

}
